package org.example.Swing.handler;

import org.example.Swing.xiangmu.login.AddKcView;
import org.example.Swing.xiangmu.login.AddView;
import org.example.controller.GradeController;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class AddKcHandler extends KeyAdapter implements ActionListener {
    private final AddKcView addKcView;
    private final AddView addView;

    public AddKcHandler(AddKcView addKcView, AddView addView) {
        this.addKcView = addKcView;
        this.addView = addView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("添加课程".equals(text)) {
            add();
        }
    }

    public void add() {
        GradeController gradeController = new GradeController();
        String name = addKcView.getKeTxt().getText();
        if (name != null) {
            int i = gradeController.insertCourse(name);
            if (i > 0) {
                JOptionPane.showMessageDialog(addKcView, "增加成功！");
                addKcView.dispose();
            } else {
                JOptionPane.showMessageDialog(addKcView, "增加失败！");
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (KeyEvent.VK_ENTER == e.getKeyCode()) {
            add();
        }

    }
}
